package com.kaikeba.wx.controller;

import com.kaikeba.bean.*;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.mvc.ResponseView;
import com.kaikeba.service.ExpressService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JsonUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 平川
 * @Description:
 * @Date Created in 2021-08-04 8:29
 * @Modified By:
 */
public class QRCodeController {
    @ResponseView("/wx/createQRCode.do")
    public String createQRCode(HttpServletRequest req, HttpServletResponse resp){
        String code = req.getParameter("code");
        String type = req.getParameter("type");
        String userphone = null;
        String qRCodeContent = null;
        // express || user
        if ("express".equals(type)){
            // 快递二维码:  表示被扫后展示单个快递
            // code
            qRCodeContent = "express_"+code;
        }else{
            // 用户二维码:  表示被扫后展示 用户所有的快递信息
            // userphone
            Courier courier = UserUtil.getWxCourier(req.getSession());
            if (courier==null){
                //是普通用户
                User user = UserUtil.getWxUser(req.getSession());
                userphone = user.getPhonenumber();
            }else{
                userphone = courier.getPhonenumber();
            }
            qRCodeContent = "userphone_"+userphone;
        }
        HttpSession session = req.getSession();
        session.setAttribute("qrcode",qRCodeContent);
        return "/personQRcode.html";
    }

    @ResponseBody("/wx/qrcode.do")
    public String getQRCode(HttpServletRequest req,HttpServletResponse resp){
        HttpSession session = req.getSession();
        String qrcode = (String)session.getAttribute("qrcode");
        Message msg = new Message();
        if (qrcode == null){
            msg.setStatus(-1);
            msg.setResult("取件码获取出错，请用户重新操作");
        }else{
            msg.setStatus(0);
            msg.setResult(qrcode);
        }
        return JsonUtil.toJSON(msg);
    }
    @ResponseBody("/wx/updateStatus.do")
    public String updateExpressStatus(HttpServletRequest req,HttpServletResponse resp){
        String code = req.getParameter("code");
        boolean flag = ExpressService.updateStatus(code);
        Message msg = new Message();
        if (flag){
            //取到件了
            msg.setStatus(0);
            msg.setResult("取件成功！");
        }else{
            //未取到件
            msg.setStatus(-1);
            msg.setResult("取件码不存在，请用户更新二维码！");
        }
        return JsonUtil.toJSON(msg);
    }
    @ResponseBody("/wx/findExpressByCode.do")
    public String findExpressByCode(HttpServletRequest req,HttpServletResponse resp){
        String code = req.getParameter("code");
        Express e1 = ExpressService.findByCode(code);
        BootStrapTableExpress e2 = null;
        Message msg = new Message();
        if (e1!=null){
            String intime = DateFormatUtil.format(e1.getIntime());
            String outtime = e1.getOuttime()==null?"未出库":DateFormatUtil.format(e1.getOuttime());
            String status = e1.getStatus()==0?"未取件":"已取件";
            e2 = new BootStrapTableExpress(e1.getId(),e1.getNumber(), e1.getUsername(),e1.getUserphone(),e1.getCompany(),e1.getCode(),intime, outtime, status,e1.getSysPhone());
            msg.setStatus(0);
            msg.setResult("查找成功！");
            msg.setData(e2);
        } else{
            //未取到件
            msg.setStatus(-1);
            msg.setResult("取件码不存在，请用户检查再输入！");
        }
        req.getSession().setAttribute("indexQueryExpress",msg);
        return JsonUtil.toJSON(msg);
    }








}
